<template>
  <div class="con_store_main">
    <!--新增窗口-->
    <el-dialog title="新增员工" :visible.sync="addBox" width="50%" :close-on-click-modal="true" center>
      <div>
        <el-form :model="addForm" :rules="rules">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="addForm.name" placeholder="输入员工姓名"></el-input>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input v-model="addForm.age" placeholder="输入员工年龄"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-select v-model="addForm.gender" placeholder="选性别">
              <el-option v-for="item in genders" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="职位" prop="position">
            <el-input v-model="addForm.position" placeholder="输入员工职位"></el-input>
          </el-form-item>
          <el-form-item label="联系方式" prop="phoneNumber">
            <el-input v-model="addForm.phoneNumber" placeholder="输入员工联系方式"></el-input>
          </el-form-item>
          <div>
            <el-button @click="confirmAdd">確認添加</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
    <!--编辑窗口-->
    <el-dialog title="修改员工信息" :visible.sync="updateBox" width="50%" :close-on-click-modal="true" center>
      <div>
        <el-form :model="updateForm" :rules="rulesEdit">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="updateForm.name" placeholder="输入员工姓名"></el-input>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input v-model="updateForm.age" placeholder="输入员工年龄"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-select v-model="updateForm.gender" placeholder="选择员工性别">
              <el-option v-for="item in genders" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="职位" prop="position">
            <el-input v-model="updateForm.position" placeholder="输入员工职位"></el-input>
          </el-form-item>
          <el-form-item label="联系方式" prop="phoneNumber">
            <el-input v-model="updateForm.phoneNumber" placeholder="输入员工联系方式"></el-input>
          </el-form-item>
          <div>
            <el-button @click="confirmUpdate">確認修改</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="员工姓名">
        <el-input v-model="formInline.name" placeholder="员工姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="findData" type="primary" icon="el-icon-search">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="addEmployee" type="primary" icon="el-icon-plus">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table class="table" :data="tableData" style="width: 100%" v-loading="loading"
      :default-sort="{ prop: 'index', order: 'descending' }">
      <el-table-column type="index" label="序号" sortable width="100">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="100">
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="60">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="60">
      </el-table-column>
      <el-table-column prop="position" label="职位" width="100">
      </el-table-column>
      <el-table-column prop="phoneNumber" label="联系方式" width="110">
      </el-table-column>
      <el-table-column prop="hireTime" label="入职时间">
      </el-table-column>
      <el-table-column prop="isActive" label="在职情况" width="100">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="editEmployee(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteEmployee(scope.row)">删除</el-button>
          <el-button type="text" size="small" @click="confirmUpdateStatus(scope.row)">切换状态</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin: auto; margin-top: 8px" :hide-on-single-page="value" :current-page.sync="currentPage"
      :page-size="6" :pager-count="11" layout="prev, pager, next" :total="this.total"
      @current-change="handleSizeChange">
    </el-pagination>
  </div>
</template>
<script>
import { request } from "@/network/request/request";
export default {
  mounted() {
    this.findData();
  },
  data() {
    return {
      rules: {
        name: [
          { required: true, message: '请输入员工名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入员工年龄', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择员工性别', trigger: 'change' }
        ],
        position: [
          { required: true, message: '请输入员工职位', trigger: 'blur' },
          { min: 1, max: 12, message: '长度在 1 到 12个字符', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入员工电话', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
      },
      rulesEdit: {
        name: [
          { required: true, message: '请输入员工名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入员工年龄', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择员工性别', trigger: 'change' }
        ],
        position: [
          { required: true, message: '请输入员工职位', trigger: 'blur' },
          { min: 1, max: 12, message: '长度在 1 到 12个字符', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入员工电话', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
      },
      addForm: {
        storeId: 0,
        name: '',
        gender: '',
        age: '',
        position: '',
        phoneNumber: '',
        isActive: '',
      },
      updateForm: {
        employeeId: 0,
        name: '',
        gender: '',
        age: '',
        position: '',
        phoneNumber: '',
        isActive: '',
      },
      genders: [{ value: '男', label: '男' }, { value: '女', label: '女' }],
      updateStatus: {
        customerId: 0,
        status: ""
      },
      formInline: { name: '' },
      updateBox: false,
      addBox: false,
      currentPage: 1, // 当前页码
      pageSize: 6, // 每页显示的记录数
      total: 0, // 总数目，默认设置为 0
      loading: false,
      value: false,
      tableData: [],

    };
  },
  methods: {
    time() {
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
      }, 600);
    },
    handleSizeChange() {
      this.findData();
    },
    deleteAlert(row) {
      this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest(row);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    editEmployee(row) {
      console.log(row);
      this.updateForm.employeeId = row.employeeId;
      this.updateForm.name = row.name;
      this.updateForm.gender = row.gender;
      this.updateForm.age = row.age;
      this.updateForm.position = row.position;
      this.updateForm.phoneNumber = row.phoneNumber;
      this.updateBox = true;
    },
    addEmployee() {
      this.addBox = true;
    },
    deleteEmployee(row) {
      this.deleteAlert(row);
    },
    async confirmUpdate() {
      const config = {
        url: 'api/user/employee',
        method: "put",
        data: {
          employeeId: this.updateForm.employeeId,
          name: this.updateForm.name,
          gender: this.updateForm.gender,
          age: this.updateForm.age,
          position: this.updateForm.position,
          phoneNumber: this.updateForm.phoneNumber,
        }
      }
      const res = await request(config);
      if (res.data.code === 1) {
        this.$message.success("修改成功");
        this.findData();
        this.updateBox = false;
      }
    },
    async confirmUpdateStatus(row) {
      this.updateStatus.employeeId = row.employeeId;
      if (row.isActive === "在职") {
        this.updateStatus.isActive = "离职";
      } else {
        this.updateStatus.isActive = "在职";
      }
      const config = {
        url: 'api/user/employee',
        method: "put",
        data: {
          employeeId: this.updateStatus.employeeId,
          isActive: this.updateStatus.isActive,
        }
      }
      const res = await request(config);
      if (res.data.code === 1) {
        this.$message.success("在职状态修改成功");
        this.findData();
      }
    },
    async findData() {
      this.time();
      const storeId = localStorage.getItem("storeId");
      if (storeId == 0) {
        this.$message.error("无店铺数据！");
        return;
      }
      const params = {
        name: this.formInline.name,
        currentPage: this.currentPage /* 当前页码 */,
        pageSize: this.pageSize /* 页码大小 */,
        storeId: localStorage.getItem("storeId"),
      };
      const config = {
        url: "api/user/employee",
        method: "get",
        params: params,
      };
      const res = await request(config);
      if (res.data.code === 1) {
        this.total = res.data.data.total; /* 得到数据的总数 */
        this.tableData = res.data.data.pageList; /* 得到数据 */
      }
    },
    async deleteRequest(row) {
      let id = row.employeeId;
      const config = {
        url: `api/user/employee/${id}`,/* 通过路径参数 路径参数 */
        method: "delete",
      }
      const res = await request(config);
      if (res.data.code === 1) {
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.findData();
      } else {
        this.$message({
          type: 'error',
          message: '删除失败'
        });
      }

    },
    async confirmAdd() {
      const storeId = localStorage.getItem("storeId");
      if (storeId == 0) {
        this.$message.error("无店铺数据！");
        return;
      }
      const config = {
        url: "api/user/employee",
        method: "post",
        data: {
          storeId: localStorage.getItem("storeId"),
          name: this.addForm.name,
          age: this.addForm.age,
          gender: this.addForm.gender,
          isActive: this.addForm.isActive,
          position: this.addForm.position,
          phoneNumber: this.addForm.phoneNumber,
        }
      }
      const res = await request(config);
      if (res.data.code == 1) {
        this.$message.success("添加成功");
        this.addForm.name = '';
        this.addForm.age = '';
        this.addForm.gender = '';
        this.addForm.position = '';
        this.addForm.phoneNumber = '';
        this.addBox = false;
      }

      this.findData();
    },

  },
};
</script>
<style scoped>
.con_store_main {
  height: 498px;
  display: flex;
  flex-direction: column;
  align-content: center;
  justify-content: center;
}

.ic-status {
  display: flex;
  align-content: center;
}
</style>